package com.ruoyi.project.tool.swagger;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.HttpClients;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.OperatorType;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Api("城市信息管理")
@RestController
@RequestMapping("/mobile")
public class CitiesController extends BaseController {

    @Autowired
    private RedisCache redisCache;

    @Autowired
    RuoYiConfig ruoYiConfig;
    @RequestMapping(value = "/cities",method = RequestMethod.GET)
    @Log(title = "城市列表", operatorType = OperatorType.MOBILE)
    public AjaxResult cities(@RequestParam("type") String type, HttpServletRequest request) {
        String cities =redisCache.getCacheObject(Constants.CITIES);
        JSONObject  myJson = JSONObject.parseObject(cities);
        Map<String, List> data = (Map) myJson;
        switch (type){
            case "hot":
                return AjaxResult.success(data.get("hotCities"));
            case "group":
                return AjaxResult.success(data);
            default:
                Map result = null;
                for (Map.Entry<String, List> entry : data.entrySet()) {
                    List list = entry.getValue();
                    for (int i = 0; i < list.size(); i++) {
                        Map rec = (Map) list.get(i);
                        if (type.equals(rec.get("name"))) {
                            result = rec;
                            break;
                        }
                    }
                };
                return  AjaxResult.success(result);
        }

    }


    @RequestMapping(value = "/searchLocation",method = RequestMethod.GET)
    public AjaxResult getPoiByCityAndKeyword(@RequestParam(value = "cityName")String cityName,
                                         @RequestParam(value = "keyword",required = false)String keyword,@RequestParam(value = "geohash",required = false)String geohash){
        Map<String, String> params = Maps.newHashMap();
        params.put("key", RuoYiConfig.getTencentKey());
        params.put("keyword", URLEncoder.encode(keyword));
        params.put("boundary", "region(" + URLEncoder.encode(cityName) + ",0)");
        params.put("page_size", "10");
        try {
            String str = HttpClients.get(RuoYiConfig.getQqApiUrl() + "place/v1/search", params);
            Map result = (Map) JSONObject.parse(str);
            if (Integer.valueOf(result.get("status").toString()).intValue() == 0) {
                return  AjaxResult.success((List) result.get("data"));
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return AjaxResult.error();
    }


}
